package com.zj.utils.utils.http;

import android.os.StrictMode;
import android.util.Log;

import com.blankj.utilcode.util.AppUtils;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.StringReader;
import java.util.LinkedHashMap;
import java.util.Map;


public class HttpConfig {

    public static String callService(int isLogin,String methodName, LinkedHashMap<String, Object> map) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
                .penaltyLog().penaltyDeath().build());
        try {
            // 命名空间
            String nameSpace;
            if(isLogin==0){
                nameSpace=NetConfig.LOGIN_MAIN+NetConfig.LOGIN_TEST;

            }else{
                nameSpace=NetConfig.MAIN+NetConfig.TEST;
            }
            // SOAP Action
            String soapAction = nameSpace + methodName;
            SoapObject request = new SoapObject(nameSpace, methodName);
            for (Map.Entry<String, Object> me : map.entrySet()) {
                // 根据键值对对象获取键和值
                request.addProperty(me.getKey(), me.getValue().toString());
                //Log.e("HttpConfig", "callService: " + me.getKey() + "     " + me.getValue().toString());
            }
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.bodyOut = request;
            envelope.setOutputSoapObject(request);
            envelope.encodingStyle = "UTF-8";
            //MyAndroidHttpTransport transport = new MyAndroidHttpTransport(nameSpace, 8000);
            HttpTransportSE transport = new HttpTransportSE(nameSpace,8000);// wsdl文档
            try {
                // 调用WebService
                transport.call(soapAction, envelope);
                // transport.call(null, envelope);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Object obj = envelope.getResponse();

            return obj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    //用Pull方式解析XML
    public static Boolean parseXMLWithPull(String xmlData) {
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = factory.newPullParser();
            //设置输入的内容
            xmlPullParser.setInput(new StringReader(xmlData));
            //获取当前解析事件，返回的是数字
            int eventType = xmlPullParser.getEventType();
            //保存内容
            String code;
            //String msg = "";

            while (eventType != (XmlPullParser.END_DOCUMENT)) {
                String nodeName = xmlPullParser.getName();
                switch (eventType) {
                    //开始解析XML
                    case XmlPullParser.START_TAG: {
                        //nextText()用于获取结点内的具体内容
                        if ("code".equals(nodeName)) {
                            code = xmlPullParser.nextText();
                            if (code.equals("success"))
                                return true;
                        }
                        "msg".equals(nodeName);
                        /*else if("version".equals(nodeName))
                            version = xmlPullParser.nextText();*/
                    }
                    break;
                    //结束解析
                    case XmlPullParser.END_TAG: {
                    }
                    break;
                    default:
                        break;
                }
                //下一个
                eventType = xmlPullParser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //TODO 文件的上传
    /*public static boolean connectWebService(String methodName, byte[] bytes) {
        String namespace = "http://127.0.0.1:8080/dems_uap/dems_webservice/services/IscService/ITpwsurveytaskService";  // 命名空间，即服务器端得接口，注：后缀没加 .wsdl，
        //服务器端我是用x-fire实现webservice接口的
        String methodName1 = "getfile";
        String url = "http://127.0.0.1:8080/dems_uap/dems_webservice/services/IscService/ITpwsurveytaskService";   //对应的url
        //以下就是 调用过程了，不明白的话 请看相关webservice文档
        SoapObject soapObject = new SoapObject(NetConfig.TEST, methodName);
          *//*map.put("userid", "40288a5c63805300016401348d511351");
                                        map.put("surverytaskid", "100532");
                                        map.put("File", allbytes.getBytes());
                                        map.put("description", "描述信息");
                                        *//*
        soapObject.addProperty("File", bytes);  //参数1   2.图片名
        Log.e("file", bytes.toString());
        soapObject.addProperty("surverytaskid", "100532");
        soapObject.addProperty("userid", "40288a5c63805300016401348d511351");   //参数2  图片字符串
        soapObject.addProperty("description", "ok？");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(soapObject);
        HttpTransportSE httpTranstation = new HttpTransportSE(namespace);
        try {
            httpTranstation.call(namespace, envelope);
            Object result = envelope.getResponse();
            Log.i("connectWebService", result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }*/

    //TODO
    /*public static String getWebService(String methodName, Map<String, Object> map) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
                .penaltyLog().penaltyDeath().build());
        try {
            // 命名空间
            String nameSpace = NetConfig.TEST;
            // SOAP Action
            String soapAction = nameSpace + methodName;
            SoapObject request = new SoapObject(nameSpace, methodName);
            for (Map.Entry<String, Object> me : map.entrySet()) {
                // 根据键值对对象获取键和值

                request.addProperty(me.getKey(), me.getValue().toString());
                Log.e("HttpConfig", "callService: " + me.getKey() + "     " + me.getValue().toString());
            }
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.bodyOut = request;
            envelope.setOutputSoapObject(request);
            envelope.encodingStyle = "UTF-8";
            HttpTransportSE transport = new HttpTransportSE(
                    NetConfig.TEST);// wsdl文档

            try {
                // 调用WebService
                transport.call(soapAction, envelope);
                // transport.call(null, envelope);
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("TAG", "callService: " + e.getMessage());
            }
            Object obj = envelope.getResponse();

            return obj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }*/


    public static String getVersion() {
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        //1.登陆接口 POST
        map.put("xqapp", "xqapp");
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
                .penaltyLog().penaltyDeath().build());
        try {
            // 命名空间
            String nameSpace = NetConfig.MAIN+NetConfig.VERSION;
            // SOAP Action
            String soapAction = nameSpace + "getSoftVersion";
            SoapObject request = new SoapObject(nameSpace, "getSoftVersion");
            for (Map.Entry<String, Object> me : map.entrySet()) {
                // 根据键值对对象获取键和值

                request.addProperty(me.getKey(), me.getValue().toString());
                Log.e("HttpConfig", "callService: " + me.getKey() + "     " + me.getValue().toString());
            }
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.bodyOut = request;
            envelope.setOutputSoapObject(request);
            envelope.encodingStyle = "UTF-8";
            HttpTransportSE transport = new HttpTransportSE(
                    nameSpace);// wsdl文档
            try {
                // 调用WebService
                transport.call(soapAction, envelope);
                // transport.call(null, envelope);
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("TAG", "callService: " + e.getMessage());
            }
            Object obj = envelope.getResponse();
            return obj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


    //1.TODO 用户登陆解析用Pull方式解析XML
    public static VersionBean parseXMLVersion(String xmlData) {
        VersionBean versionBean = new VersionBean();
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = factory.newPullParser();
            //设置输入的内容
            xmlPullParser.setInput(new StringReader(xmlData));
            //获取当前解析事件，返回的是数字
            int eventType = xmlPullParser.getEventType();
            //保存内容
            String softVersion;
            String softPath;

            while (eventType != (XmlPullParser.END_DOCUMENT)) {
                String nodeName = xmlPullParser.getName();
                switch (eventType) {
                    //开始解析XML
                    case XmlPullParser.START_TAG: {
                        //nextText()用于获取结点内的具体内容
                        if ("softVersion".equals(nodeName)) {
                            softVersion = xmlPullParser.nextText();
                            String name = AppUtils.getAppVersionName();
                            if (softVersion.equals(name)) {
                                versionBean.setUpdate(false);
                            } else
                                versionBean.setUpdate(true);
                        }
                        if ("softPath".equals(nodeName)) {
                            softPath = xmlPullParser.nextText();
                            versionBean.setUpdateUrl(softPath);
                        }
                    }
                    break;
                    //结束解析
                    case XmlPullParser.END_TAG: {
                    }
                    break;
                    default:
                        break;
                }
                //下一个
                eventType = xmlPullParser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionBean;
    }


}
